package chatclient;


import chatqueue.ChatQueuePackage;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

public class GUI {
    // I WANT TO COMMIT
    private JFrame main;

    private JTabbedPane tabbedPane = new JTabbedPane();

    private JButton nextClient = new JButton("Ny klient");

    private MessageService mService;

    public ArrayList<Chat> chats = new ArrayList<Chat>();

    public GUI(MessageService mService) {
        main = new JFrame("ChatMaster 2000");
        this.mService = mService;
        main.setSize(720, 480);
        main.setDefaultCloseOperation(main.EXIT_ON_CLOSE);
        main.setResizable(false);
        main.getContentPane().setLayout(new BorderLayout());

        nextClient.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                    newClient();
            }
        });

        main.getContentPane().add(nextClient, BorderLayout.NORTH);
        main.getContentPane().add(tabbedPane, BorderLayout.CENTER);

        main.setVisible(true);
    }

    public void newMessage(Message message) throws JMSException {
            ObjectMessage msg = (ObjectMessage) message;
            ChatQueuePackage cqp = (ChatQueuePackage) msg.getObject();

            if(cqp.queueNumber > 0) {
                // Oppdater kønummer
            }

            if(message.getJMSReplyTo() == null)
                return;

            boolean exist = false;
            for(Chat chat: chats)
                if(chat.equals(message.getJMSReplyTo())) {
                    exist = true;
                }
            if(!exist) {
                addChat(message.getJMSReplyTo());
                for(Chat chat: chats) {
                    if(chat.equals(message.getJMSReplyTo())) {
                        tabbedPane.addTab("Chat", null, chat);
                        main.doLayout();
                    }
                }
            }
            if(cqp.text != null) {
                for(Chat chat: chats)
                    if(chat.equals(message.getJMSReplyTo())) {
                        chat.addStrangersLineToChat(cqp.text);
                        return;
                    }
            }
    }

    public void addChat(Destination destination) {
        chats.add(new Chat(this, mService, destination));
    }

    public void newClient() {
        mService.getNextInQueue();
    }

    public void closeChat(Chat chat) {
        chats.remove(chat);
        tabbedPane.remove(chat);
        chat = null;
    }
}
